//
//  ECLocalFilePath.m
//  ECERP_Car
//
//  Created by huanggulong on 16/3/3.
//  Copyright © 2016年 com.mamizx. All rights reserved.
//

#import "GLLocalFilePath.h"

@implementation GLLocalFilePath


+(NSString *)getResourcePathWithType:(GLResourcePathType)type{
    NSString *path = nil;
    switch (type) {
        case GLResourcePathTypeImage:
        {
            path = [self getImagePath];
            break;
        }
        case GLResourcePathTypeAudio:
        {
            path = [self getAudioPath];
            break;
        }
        case GLResourcePathTypeVideo:
        {
            path = [self getVideoPath];
            break;
        }case GLResourcePathTypeTemp:
        {
            path = [self getTempPath];
            break;
        }
        case GLResourcePathTypeOther:
        {
            path = [self getOtherPath];
            break;
        }
        case GLResourcePathTypeVoice:
        {
            path = [self getVoicePath];
            break;
        }case GLResourcePathTypeEnglish:{
            path = [self getEnglishPath];
            break;
        }
        default:
            break;
    }
    return path;
}

+(BOOL)createFileDirectory:(NSString *)url{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL existed = [fileManager fileExistsAtPath:url isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        return  [fileManager createDirectoryAtPath:url withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return NO;
}

+(NSString *)getImagePath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file/image"];
    [self createFileDirectory:url];
    return url;
}

+(NSString *)getVideoPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file/video"];
    [self createFileDirectory:url];
    return url;
}

+(NSString *)getAudioPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file/audio"];
    [self createFileDirectory:url];
    return url;
}

+(NSString *)getTempPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file/temp"];
    [self createFileDirectory:url];
    return url;
}

+(NSString *)getOtherPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file/other"];
    [self createFileDirectory:url];
    return url;
}

+(NSString *)getVoicePath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file/voice"];
    [self createFileDirectory:url];
    return url;
}


+(NSString *)getEnglishPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file/english"];
    [self createFileDirectory:url];
    return url;
}


+(NSString *)getFilePath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * url =[documentPath stringByAppendingPathComponent:@"file"];
    [self createFileDirectory:url];
    return url;
}

+(NSString *)getDocumentPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    return documentPath;
}

+(BOOL)existFilePath:(NSString *)path{
    NSFileManager* manager = [NSFileManager defaultManager];
    return [manager fileExistsAtPath:path];
}

+(BOOL)moveFileFrom:(NSString *)fromPath to:(NSString *)toPath{
    NSFileManager *manager = [NSFileManager defaultManager];
    return [manager moveItemAtPath:fromPath toPath:toPath error:nil];
}

+(BOOL)deleteFilePath:(NSString *)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    return [manager removeItemAtPath:filePath error:nil];
}

+(BOOL)cleanFileCache{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    return [fileManager removeItemAtPath:[self getFilePath] error:&error];
}

+(BOOL)cleanTempCache{
    if ([self folderSizeAtPath:[self getTempPath]] > 10 * 1024 * 1024) {//临时文件超过10M就清理一次
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        return [fileManager removeItemAtPath:[self getTempPath] error:&error];
    }else{
        return NO;
    }
}

+(long long)getFileCacheSize{
    return [self folderSizeAtPath:[self getFilePath]];
}

+ (long long)folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        BOOL isDir = false;
        [manager fileExistsAtPath:fileAbsolutePath isDirectory:&isDir];
        if (isDir) {
            folderSize += 0.0;
        }else{
            folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
    }
    return folderSize;
}

+(long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}


@end
